package com.southwind.springboottest.study.Java中通讯.tcp协议案例;

import java.io.*;
import java.net.Socket;

public class TCPClient {
    public static void main(String[] args) {
        String serverAddress = "localhost"; // 服务器地址
        int port = 12345; // 服务器端口号
        try (Socket socket = new Socket(serverAddress, port);
             PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
             BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()))) {

            // 发送消息到服务器
            out.println("Hello, TCP Server!");

            // 读取服务器返回的消息
            String response = in.readLine();
            System.out.println("Server response: " + response);
        } catch (IOException e) {
            e.printStackTrace(); // 处理 I/O 异常
        }
    }
}

